﻿using System;
using System.Collections.Generic;
using System.IO;
using EmployeeApp.DAL.Dal;
using EmployeeApp.DAL.Entities;
using EmployeeApp.DAL.Models.Position;
using EmployeeApp.Service.Dto.Position;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace EmployeeApp.Service
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "PositionService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select PositionService.svc or PositionService.svc.cs at the Solution Explorer and start debugging.
    public class PositionService : IPositionService
    {
        public ISession Session;
        readonly ISessionFactory _sessionFactory;
        public PositionService()
        {
            var appReader = new System.Configuration.AppSettingsReader();
            var connectionString = Convert.ToString(appReader.GetValue("connectionString", typeof(string)));
            var mappingScriptPath = AppDomain.CurrentDomain.BaseDirectory + "/db_mappings";
            if (!Directory.Exists(mappingScriptPath))
                Directory.CreateDirectory(mappingScriptPath);

            _sessionFactory = Fluently.Configure()
                .Database(
                    MsSqlConfiguration.MsSql2008
                    .ConnectionString(@connectionString))
                    .Mappings(x => x.AutoMappings.Add(AutoMap.AssemblyOf<Position>().Where(t => t.Namespace.EndsWith("Entities"))).ExportTo(@mappingScriptPath))
                //.ExposeConfiguration(BuildSchema) // ********** ใช้ครั้งแรกเท่านั้น (กรณียังไม่มี Table)
                    .BuildSessionFactory();
            Session = _sessionFactory.OpenSession();
        }
        private static void BuildSchema(Configuration config)
        {
            new SchemaExport(config).Create(false, true);
        }

        public ResponseDto<List<PositionDto>> GetAll(GetAllRequestDto arg)
        {
            var result = new ResponseDto<List<PositionDto>>();
            try
            {
                var dal = new PositionDAL(Session);

                SearchCriteria search = null;
                if (arg != null)
                {
                    search = new SearchCriteria
                    {
                        Code = arg.Code,
                        Name = arg.Name,
                        Sort = arg.Sort,
                        SortType = arg.SortType,
                    };
                }

                var list = dal.GetAll(search);
                result.Data = new List<PositionDto>();
                if (list.Count > 0)
                {
                    foreach (var item in list)
                    {
                        var obj = new PositionDto
                        {
                            Id = item.Id,
                            Code = item.Code,
                            Name = item.Name,
                        };

                        result.Data.Add(obj);
                    }
                }
                result.Status = true;
            }
            catch (Exception ex)
            {
                result.Status = false;
                result.ErrorMessage = ex.Message;
            }
            return result;
        }
    }
}
